// src/Factory.hpp
#ifndef FACTORY_HPP
#define FACTORY_HPP

#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <stdexcept>

template<typename CreatorFunc>
class Factory {
public:
    // 注册：将标签映射到创建函数
    void subscribe(const std::string& tag, CreatorFunc creator) {
        auto result = creators_.emplace(tag, std::move(creator));
        if (!result.second) {
            throw std::runtime_error("Factory tag 重复注册: " + tag);
        }
    }

    // 创建：根据标签和参数，调用对应的创建器
    template<typename... Args>
    auto create(const std::string& tag, Args&&... args) const {
        auto it = creators_.find(tag);
        if (it == creators_.end()) {
            throw std::runtime_error("未知的 Factory tag: " + tag);
        }
        return (it->second)(std::forward<Args>(args)...);
    }

private:
    std::unordered_map<std::string, CreatorFunc> creators_;
};

#endif // FACTORY_HPP